package com.hongruan.action.Settings;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hongruan.action.BaseAction;
import com.hongruan.bean.Swjg;
import com.hongruan.model.ResultStatusModel;
import com.hongruan.service.SwjgService;

/**
 * 菜单相关操作
 * 
 * @author ZhangWenLong
 * 
 */
@Controller("SettingsAction")
@Scope("prototype")
public class SettingsAction extends BaseAction {

	private static final long serialVersionUID = -606089158706439830L;

	private static Logger logger = Logger.getLogger(SettingsAction.class);

	@Resource
	private SwjgService swjgService;
	
	private Swjg swjg;
	
	public String swjgList(){
		logger.info("swjgList() begin");
		
		JSONObject json = new JSONObject();
		ResultStatusModel result = new ResultStatusModel();

		try {
			List<Swjg> swjgList = this.swjgService.swjgList(swjg);

			json.put("swjgList", swjgList);
			result.setSuccess(true);

		} catch (Exception e) {
			logger.error("swjgList()... error", e);

			result.setSuccess(false);
		}

		json.put("result", result);

		this.writeAjaxResult(json.toString());

		return null;
	}
	
	public String addOrUpdateSwjg(){
		logger.info("addOrUpdateSwjg() begin");
		
		String modifyFlag = this.getRequest().getParameter("modifyFlag");
		
		JSONObject json = new JSONObject();
		ResultStatusModel result = new ResultStatusModel();

		try {
			if("1".equals(modifyFlag)){
				Swjg s = this.swjgService.read(swjg.getId());
				s.setBm(swjg.getBm());
				s.setMc(swjg.getMc());
				s.setJc(swjg.getJc());
				s.setQc(swjg.getQc());
				s.setJb(swjg.getJb());
				s.setSx(swjg.getSx());
				s.setQybz(swjg.getQybz());
				s.setJj(swjg.getJj());
				this.swjgService.update(s);
			}else{
				swjg.setIsDel("0");
				this.swjgService.create(swjg);
			}

			json.put("swjgId", swjg.getId());
			result.setSuccess(true);

		} catch (Exception e) {
			logger.error("addOrUpdateSwjg()... error", e);

			result.setSuccess(false);
		}

		json.put("result", result);

		this.writeAjaxResult(json.toString());

		return null;
	}
	
	public String deleteSwjg(){
		logger.info("deleteSwjg() begin");
		
		JSONObject json = new JSONObject();
		ResultStatusModel result = new ResultStatusModel();

		try {
			swjg = this.swjgService.read(swjg.getId());
			swjg.setIsDel("1");
			this.swjgService.update(swjg);
			
			result.setSuccess(true);

		} catch (Exception e) {
			logger.error("deleteSwjg()... error", e);

			result.setSuccess(false);
		}

		json.put("result", result);

		this.writeAjaxResult(json.toString());

		return null;
	}
	
	public Swjg getSwjg() {
		return swjg;
	}

	public void setSwjg(Swjg swjg) {
		this.swjg = swjg;
	}
}
